package com.apptest.android.core.network;

import android.util.Log;
import androidx.annotation.NonNull;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final String TAG = "BaseObserver";

    public abstract void onSuccess(T t);

    public void onFailure(int code, String message) {
        Log.d(TAG, message);
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        Log.d(TAG, "onSubscribe");
    }

    @Override
    public void onNext(@NonNull BaseResponse<T> response) {
        if (response.getErrorCode() == BaseResponse.SUCCESS) {
            Log.d(TAG, "onSuccess");
            onSuccess(response.getData());
        } else {
            Log.d(TAG, "onFailure");
            onFailure(response.getErrorCode(), response.getErrorMsg());
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        Log.d(TAG, e.toString());
    }

    @Override
    public void onComplete() {
        Log.d(TAG, "onComplete");
    }
}
